#!/usr/bin/python
import os
import glob
import os
import platform
import imp
import os, os.path
import re
import sys
import glob

wxconfig = imp.load_source('*', 'tools/wxconfig.py')
pkgconfig = imp.load_source('*', 'tools/pkgconfig.py')
if None in (wxconfig, pkgconfig):
    print " failed to Tools."
    exit(1)

def Check_Prerequisites(env):
    # begin tests for various requirements.
    Config_Tests = { 'CheckPKGConfig' : pkgconfig.CheckPKGConfig,
                     'CheckPKG' : pkgconfig.CheckPKG,
                     'CheckWXConfig' : wxconfig.CheckWXConfig }
    conf = env.Configure( custom_tests = Config_Tests)
    #pkg-config check...
    if conf.CheckPKGConfig('0.22'):
        # OpenAL Checks
        if conf.CheckPKG('openal >= 1.12.800'):
            conf.env.ParseConfig('pkg-config --cflags --libs openal')
            conf.env.Append(CPPDEFINES = 'HAS_OPENAL_H')
            conf.env.Append(OPENAL = '1')
        else:
            conf.env.Append(OPENAL = '0')
        # FFMPEG Checks.
        if conf.CheckPKG('libavcodec >= 50.20.1'):
            conf.env.ParseConfig('pkg-config --cflags --libs libavcodec')
            conf.env.Append(CPPDEFINES = 'HAS_AVCODEC_H')
        if conf.CheckPKG('libavformat >= 52.31.0'):
            conf.env.ParseConfig('pkg-config --cflags --libs libavformat')
            conf.env.Append(CPPDEFINES = 'HAS_AVFORMAT_H')
        if conf.CheckPKG('libavdevice >= 50.1.0'):            
            conf.env.ParseConfig('pkg-config --cflags --libs libavdevice')
            conf.env.Append(CPPDEFINES = 'HAS_AVDEVICE_H')
        if conf.CheckPKG('libavutil >= 49.15.0'):
            conf.env.ParseConfig('pkg-config --cflags --libs libavutil')
            conf.env.Append(CPPDEFINES = 'HAS_AVUTIL_H')
        # LibSDL Checks.
        if conf.CheckPKG('sdl >= 1.2.14'):
            conf.env.ParseConfig('pkg-config --cflags --libs sdl')
            conf.env.Append(CPPDEFINES = 'HAS_SDL_H')
            conf.env.Append(SDL = '1')
            if conf.CheckPKG('SDL_image >= 1.2.10'):
                conf.env.ParseConfig('pkg-config --cflags --libs SDL_image')
                conf.env.Append(CPPDEFINES = 'HAS_SDL_IMAGE_H')
                conf.env.Append(SDLIMAGE = '1')
        # OpenGL, GLUT and Glew Checks.
        if conf.CheckPKG('gl >= 7.7.0'):
            conf.env.ParseConfig('pkg-config --cflags --libs gl')
            conf.env.Append(CPPDEFINES = 'HAS_GL_H')
            conf.env.Append(OPENGL = '1')
            if conf.CheckPKG('glu >= 7.7.0'):
                conf.env.ParseConfig('pkg-config --cflags --libs glu')
                conf.env.Append(CPPDEFINES = 'HAS_GLU_H')
            if conf.CheckPKG('glew >= 1.5.2'):
                conf.env.ParseConfig('pkg-config --cflags --libs glew')
                conf.env.Append(CPPDEFINES = 'HAS_GLEW_H')
        # QT 4.6 and Higher Check.
        if conf.CheckPKG('QtGui >= 4.6.0'):
            conf.env.ParseConfig('pkg-config --cflags --libs QtGui')
            conf.env['QTGUI'] = '1'
            if conf.CheckPKG('QtOpenGL >= 4.6.0'):
                conf.env.ParseConfig('pkg-config --cflags --libs QtOpenGL')
                conf.env['QTOPENGL'] = '1'
    else:
        if conf.CheckLibWithHeader( 'openal', 'AL/al.h', 'c++', '' ):
            conf.env.Append(CPPDEFINES = 'HAS_AL_H')
            conf.env['OPENAL'] = '1'
        elif conf.CheckLibWithHeader( 'openal', 'OpenAL/al.h', 'c++', '' ):
            conf.env.Append(CPPDEFINES = 'HAS_OPENAL_H')
            conf.env['OPENAL'] = '1'
    # Begin wxconfig test.
    if conf.CheckWXConfig('2.8', ['gl', 'adv', 'core', 'base'], False):
        wxconfig.ParseWXConfig(conf.env)
    # end wxconfig test.
    # begin pawn preparations.
    if conf.CheckCHeader( 'unistd.h' ):
        conf.env.Append(CPPDEFINES = 'HAVE_UNISTD_H')
    if conf.CheckCHeader( 'stdint.h' ):
        conf.env.Append(CPPDEFINES = 'HAVE_STDINT_H')
    if conf.CheckCHeader( 'inttypes.h' ):
        conf.env.Append(CPPDEFINES = 'HAVE_INTTYPES_H')
    if conf.CheckCHeader( 'inttypes.h' ):
        conf.env.Append(CPPDEFINES = 'HAVE_INTTYPES_H')
    if conf.CheckCHeader( 'curses.h'):
        conf.env.Append(CPPDEFINES = 'HAVE_CURSES_H')
        conf.env.Append(LIBS = 'ncurses')
    if conf.CheckCHeader( 'alloca.h'):
        conf.env.Append(CPPDEFINES = 'HAVE_ALLOCA_H')
    if conf.CheckCHeader( 'dlfcn.h'):
        conf.env.Append(CPPDEFINES = 'HAVE_DLFCN_H')

     #end Pawn preparations.
